package com.ng.framework.view.desktop.filters;

import java.util.List;

import com.ng.framework.persistence.NGListingBean;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.NGCombo;

public class NGComboFilter extends NGSimpleFilter<NGCombo> {

	private static final long serialVersionUID = -1501728224620772930L;
	
	public NGComboFilter(String name, Class<?> clazz, String attribute,
			Type type, UnaryOperation unaryOperation, List<NGListingBean> comboContent) {
		this(name, clazz, attribute, type, unaryOperation, null, comboContent);
	}
	
	public NGComboFilter(String name, Class<?> clazz, String attribute,
			Type type, UnaryOperation unaryOperation, String joinAttribute, List<NGListingBean> comboContent) {
		super(name, clazz, attribute, type, unaryOperation, joinAttribute);
		
		comboContent.add(0, null);
		this.getComponent().addNGItems(comboContent);
	}

	@Override
	protected NGCombo createComponent() {
		NGCombo combo = new NGCombo();
		combo.setPrototypeDisplayValue("W");
		BoundsPopupMenuListener listener = new BoundsPopupMenuListener(true, false);
		combo.addPopupMenuListener(listener);
		return combo;
	}

	@Override
	protected String getComponentValue(NGCombo component) {
		String componentValue = null;
		
		NGListingBean selectedNGSelectedBean = component.getSelectedNGItem(NGListingBean.class);
		if(selectedNGSelectedBean != null) {
			componentValue = String.valueOf(selectedNGSelectedBean.getId());
		}
		
		return componentValue;
	}

	@Override
	public void clear() {
		this.getComponent().setSelectedItem(null);
	}

}
